<?php

class Admin_Form_Publicacao extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $id = $front->getRequest()->getParam('id', 0);
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $this->setName('form_'.$controller);
        
        Zend_Loader::loadClass('dominio','library');
        
        $disabled = '';
        if($id > 0){
           $query = 'SELECT SQL_CACHE * FROM PUBLICACAO WHERE PBC_ID = ' . $id;
           $data = $db->fetchRow($query);
           $disabled = 'disabled="disabled"';
        }
        
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $_PER_ID = $s_user->read()->PER_ID;
        $_USU_ID = $s_user->read()->USU_ID;
        
        $where = '';
        
        if($_PER_ID <> 1){
            $query = 'SELECT SQL_CACHE PRS_ID FROM PROFESSOR WHERE USU_ID = '.$_USU_ID;
            $prf = $db->fetchRow($query);
        
            $where .= ' AND PRS_ID = '.$prf['PRS_ID'];
        }
        
        
        
        $el = new Zend_Form_Element_Hidden('PBC_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        
        $arr_set = array();
        foreach(explode(',',$data['TPC_ID']) as $arr_ex) $arr_set[$arr_ex] = $arr_ex;
        
        $htmli = '<select id="TPC_ID[]" name="TPC_ID[]" class="chosen-select" multiple="multiple" style="width:500px;" '.$disabled.'>';
            
        $query = 'SELECT SQL_CACHE TPC_ID, TPC_NOM FROM TOPICO WHERE TPC_STA = 1 '.$where.' ORDER BY TPC_ORD,TPC_NOM';
        $row = $db->fetchAll($query); 
        foreach($row as $r){
            $selected = array_search($r['TPC_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$r['TPC_ID'].'">'.$r['TPC_NOM'].'</option>'; 
        }
        $htmli .= '</select>';
        $style = $data['TPC_ID'] > '' ? 'style="display:block !important;"':'';
        $html = '<div><label>Tópicos</label>';
        $html .= '<div id="topicos" '.$style.'>'.$htmli.'</div></div>';

        $el = new Zend_Form_Element('TPC_IDS',array( 'helper'=>'formNote', 'value'=> $html));
        //$el->setLabel('Tópicos');
        $this->addElement($el);
        
        
        $arr_set = array();
        foreach(explode(',',$data['CBS_ID']) as $arr_ex) $arr_set[$arr_ex] = $arr_ex;
        
        $htmli = '<select id="CBS_ID" name="CBS_ID[]" item="rd_cbs" class="chosen-select" multiple="multiple" style="width:500px;" '.$disabled.'>';
            
        $arr = array('0'=>'Todos Cursos');
        $query = 'SELECT SQL_CACHE CBS_ID, CBS_NOM FROM CURSOBASE WHERE CBS_STA = 1 ORDER BY CBS_ORD ASC';
        $row = $db->fetchAll($query);
        foreach($row as $c){
            $selected = array_search($c['CBS_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$c['CBS_ID'].'">'.$c['CBS_NOM'].'</option>'; 
        }
        $htmli .= '</select>';
        $style = $data['CBS_ID'] > '' ? '':'';
        $html = '<div><label><input type="radio" '.$checked.' id="rd_cbs" class="seg_ck" name="destino" value="c" '.$disabled.'> Cursos<label>';
        $html .= '<div id="cursos" class="destino" '.$style.'>'.$htmli.'</div></div>';

        $el = new Zend_Form_Element('CBS_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        //$el->setLabel('Cursos');
        $this->addElement($el);
        
        
        $arr_set = array();
        foreach(explode(',',$data['CUR_ID']) as $arr_ex) $arr_set[$arr_ex] = $arr_ex;
        
        $htmli = '<select id="CUR_ID" name="CUR_ID[]" item="rd_cur" class="chosen-select" multiple="multiple" style="width:500px;" '.$disabled.'>';
            
        $arr = array('0'=>'Todas Turmas');
        $query = 'SELECT SQL_CACHE c.CUR_ID, t.NRT_NOM FROM CURSO c, NUMEROTURMA t  WHERE c.CUR_TRM = t.NRT_ID AND c.CUR_STA = 1 and c.CUR_ANO >= "'.date('Y').'" ORDER BY t.NRT_SEM ASC, t.NRT_NOM ASC, c.CUR_NOM';
        $row = $db->fetchAll($query);
        foreach($row as $c){
            $selected = array_search($c['CUR_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$c['CUR_ID'].'">'.dominio::getTurma($c['CUR_ID'],0).'</option>'; 
        }
        $htmli .= '</select>';
        $style = $data['CUR_ID'] > '' ? '':'';
        $html = '<div><label><input type="radio" '.$checked.' id="rd_cur" class="seg_ck" name="destino" value="g" '.$disabled.'>Turmas<label>';
        $html .= '<div id="grupos" class="destino" '.$style.'>'.$htmli.'</div></div>';

        $el = new Zend_Form_Element('CUR_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        //$el->setLabel('Grupos');
        $this->addElement($el); 
        
        
        $arr_set = array();
        foreach(explode(',',$data['ALU_ID']) as $arr_ex) $arr_set[$arr_ex] = $arr_ex;
        
        $htmli = '<select id="ALU_ID" name="ALU_ID[]" item="rd_alu" class="chosen-select" multiple="multiple" style="width:500px;" '.$disabled.'>';
            
        $query = 'SELECT SQL_CACHE ALU_ID, ALU_NOM FROM ALUNO WHERE ALU_STA = 1 ORDER BY ALU_NOM';
        $row = $db->fetchAll($query); 
        foreach($row as $r){
            $selected = array_search($r['ALU_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$r['ALU_ID'].'">'.$r['ALU_NOM'].'</option>'; 
        }
        $htmli .= '</select>';
        $style = $data['ALU_ID'] > '' ? 'style="display:block !important;"':'';
        $html = '<div><label><input type="radio" '.$checked.' id="rd_alu" class="seg_ck" name="destino" value="a" '.$disabled.'> Alunos</label>';
        $html .= '<div id="alunos" class="destino" '.$style.'>'.$htmli.'</div></div>';

        $el = new Zend_Form_Element('ALU_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        //$el->setLabel('Alunos');
        $this->addElement($el);
        
        
        if($id == 0){
            $btn = new Zend_Form_Element_Submit('Salvar',array('disableLoadDefaultDecorators' =>true));
            $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
            $btn->setAttrib('id', 'submitbutton');
            $btn->setAttrib('class', 'btn-frm');
            $this->addElement($btn);
        }
         
        $btn = new Zend_Form_Element_Button('Voltar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
    }


}

